/*
 * @Author: yayalee yangcheng960813@163.com
 * @Date: 2023-10-31 19:44:05
 * @LastEditTime: 2023-11-03 22:34:22
 * @LastEditors: yayalee yangcheng960813@163.com
 * @Description: 
 * @FilePath: \double-11-war\assets\script\dialog\HomeDialog.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */
import { _decorator, instantiate, Node, Prefab, ScrollView, tween } from 'cc';
import DialogBase from '../../cocos-libs/_cmpt/ui/base/DialogBase';
import _js from '../../cocos-libs/_/_js';
import { bind } from '../../cocos-libs/_/_decorators';
import { Message } from '../item/Message';
import messageCfg from '../data/MessageConfig';
import _event, { eventsOnEnable, preloadEvent } from '../../cocos-libs/_/_event';
import { EventName } from '../default/Default';
import { user } from '../data/UserInfo';
import GloablConfig from '../data/GloablConfig';
import shopCfg from '../data/ShopConfig';
import _random from '../../cocos-libs/_/_random';
const { ccclass, property } = _decorator;

@eventsOnEnable()
@ccclass('HomeDialog')
export class HomeDialog extends DialogBase {

    @bind({ name: "ScrollView" })
    private scrollView!: ScrollView;
    @bind({ name: "ProgressLayout" })
    private progressLayout!: Node;
    @bind()
    private content!: Node;

    @property(Prefab)
    private message!: Node;

    private userStateDictionaries: Record<string, Node> = {};

    protected onLoad(): void {
        super.onLoad();
        _js.each(this.progressLayout.children, node => {
            this.userStateDictionaries[node.name] = node;
        });
        this.updateProgress();
        this.node.click(() => {
            // console.error(shopCfg.getShuffleDate(3));
            _event.emit(EventName.REFRESH_USER_STATE);
        })
    }

    async start() {
        while (this.content.childCount < 10 && this.isValid) {
            let node = instantiate(this.message);
            let seed = _random.getSeed();
            let src = node.$(Message);
            //DEBUG : 调用时机比 onLoad 还快 导致 @bind 报错
            src.init(messageCfg.random(), seed);
            this.content.addChild(node);
            await this.waitCmpt(2);
            return;
        }
    }

    @preloadEvent(EventName.REFRESH_USER_STATE)
    async updateProgress() {
        // let max = 0;
        // let time = 10;
        // _js.each(this.progressLayout.children, async (node, index) => {
        //     let random = Math.random() * time;
        //     max = Math.max(random, max);
        //     let progressBar = node.progressBar;
        //     tween(progressBar).to(1, { progress: 0 }).start();
        //     await this.waitCmpt(1);
        //     tween(progressBar).to(random, { progress: 1 }).start();
        // });
        // await this.waitCmpt(max + 1);
        // this.updateProgress();
        _js.forIn(this.userStateDictionaries, node => {
            let key = node.name;
            let bar = node.progressBar;
            let progress = user.get(key, 0) / _js.get(GloablConfig, key, 0)
            tween(bar).to(1, { progress }).start();
        })
    }

}


